﻿package baidu.ui.controls
{
    import flash.events.*;

    public class RadioButton extends LabelButton
    {
        protected var _group:RadioButtonGroup;
        protected var defaultGroupName:String = "RadioButtonGroup";
        public static var defaultStyles:Object = {iconSkin:"RadioButton_Skin"};

        public function RadioButton()
        {
            this.groupName = this.defaultGroupName;
            return;
        }// end function

        override public function get classStyles() : Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }// end function

        override public function get toggle() : Boolean
        {
            return true;
        }// end function

        override public function set toggle(param1:Boolean) : void
        {
            trace("Warning: 不要修改 RadioButton 的 toggle 属性.");
            return;
        }// end function

        override public function get autoRepeat() : Boolean
        {
            return false;
        }// end function

        override public function set autoRepeat(param1:Boolean) : void
        {
            return;
        }// end function

        override public function get selected() : Boolean
        {
            return super.selected;
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            if (param1 == false || this.selected)
            {
                return;
            }
            if (this._group != null)
            {
                this._group.selection = this;
            }
            else
            {
                super.selected = param1;
            }
            return;
        }// end function

        public function get groupName() : String
        {
            return this._group == null ? (null) : (this._group.name);
        }// end function

        public function set groupName(param1:String) : void
        {
            if (this._group != null)
            {
                this._group.removeRadioButton(this);
                this._group.removeEventListener(Event.CHANGE, this.handleChange);
            }
            this._group = param1 == null ? (null) : (RadioButtonGroup.getGroup(param1));
            if (this._group != null)
            {
                this._group.addRadioButton(this);
                this._group.addEventListener(Event.CHANGE, this.handleChange, false, 0, true);
            }
            return;
        }// end function

        public function get group() : RadioButtonGroup
        {
            return this._group;
        }// end function

        public function set group(param1:RadioButtonGroup) : void
        {
            this.groupName = param1.name;
            return;
        }// end function

        override protected function initUI() : void
        {
            super.initUI();
            super.toggle = true;
            super.autoSize = true;
            return;
        }// end function

        protected function handleChange(event:Event) : void
        {
            super.selected = this._group.selection == this;
            dispatchEvent(new Event(Event.CHANGE, true));
            return;
        }// end function

    }
}
